package com.aohe.youzi.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;


import com.aohe.youzi.R;
import com.aohe.youzi.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public abstract class BaseFragment<VB extends ViewBinding> extends Fragment {
    public Activity mContext;
    protected View mRootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();

        mRootView = getViewBinding().getRoot();
        EventBus.getDefault().register(this);
        View statusBar = mRootView.findViewById(R.id.statusBarView);
        ViewGroup.LayoutParams layoutParams = statusBar.getLayoutParams();
        layoutParams.height = getStatusBarHeight();
        initView(savedInstanceState);
        initData();
        return mRootView;

    }

    /**
     * 利用反射获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * bind layout resource file
     */
    protected abstract VB getViewBinding();

    /**
     * 步骤一：初始化View，比如findViewById等操作
     */
    protected abstract void initView(Bundle savedInstanceState);

    protected abstract void initData();

    public void showToast(String msg) {
        ToastUtil.showToast(msg);
    }

    public void showToast(int id) {
        ToastUtil.showToast(id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
